package com.xiaoke.liantongdemo.pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.AcroFields.FieldPosition;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PRAcroForm;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.xiaoke.liantongdemo.bean.BusinessInput;
import com.xiaoke.liantongdemo.bean.BusinessInputs;
import com.xiaoke.liantongdemo.bean.BusinessPage;
import com.xiaoke.liantongdemo.constants.Constants;

public class PDFUtil {
	
	public static void fillPDF(BusinessInputs businessInputs, String srcPDFPath, String toPDFPath) throws Exception{		
        
        File file = new File(toPDFPath);
        if(file.exists()){
        	file.delete();
        }
        file.createNewFile();
		
//		Font helvetica = new Font(Font.FontFamily.HELVETICA, 12);
//        BaseFont bf_helv = helvetica.getCalculatedBaseFont(false);     
        
//        BaseFont bf_helv = BaseFont.createFont("STSong-Light",    
//				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  

//				Font font = new Font(bfComic, 9, Font.NORMAL); 
      
        PdfReader reader = new PdfReader(srcPDFPath);       
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(toPDFPath));
        AcroFields acroFields = stamper.getAcroFields();
//        PRAcroForm acroForm = reader.getAcroForm();
        
        ArrayList<BusinessPage> businessPageList = businessInputs.getBusinessPageList();
        int pageSize = businessPageList.size();
        for(int i = 0; i < pageSize; i ++ ){
        	
        	BusinessPage page = businessPageList.get(i);
        	ArrayList<BusinessInput> businessInputList = page.getBusinessInputList();
        	for(BusinessInput businessInput : businessInputList){
        		int type = businessInput.getType();
        		if(type == BusinessInput.Type.TEXT){
//        			if(businessInput.getName().equals("user_name") || businessInput.getName().equals("FirstMonth")){
//            			FieldInformation info = acroForm.getField(businessInput.getName());
//        				System.out.println(info.getInfo().getKeys());
//        				for(PdfName name : info.getInfo().getKeys()){
//        					System.out.println("name = "+ name + "  value = " + info.getInfo().get(name) + " class = " + info.getInfo().get(name).getClass());
//        				}
//        			}
//        			
//        			List<FieldPosition> positionList = acroFields.getFieldPositions(businessInput.getName());
//        			acroFields.removeField(businessInput.getName());
//        			for(FieldPosition fieldPosition : positionList){
//        				PdfContentByte canvas =  stamper.getOverContent(fieldPosition.page);
//        				canvas.beginText();
//        				canvas.setFontAndSize(bf_helv, 12);
//        				canvas.showTextAligned(Element.ALIGN_LEFT, businessInput.getInput(), fieldPosition.position.getLeft(),
//    					   fieldPosition.position.getBottom(), 0);
//        				canvas.endText();
//        			}
        			acroFields.setField(businessInput.getName(), businessInput.getInput());
            		acroFields.setFieldProperty(businessInput.getName(), "fflags", PdfFormField.FF_READ_ONLY, null);
        		} else if(type == BusinessInput.Type.CHOICE){
//        			FieldInformation info = acroForm.getField(businessInput.getName());
//    				System.out.println(info.getInfo().getKeys());
//    				for(PdfName name : info.getInfo().getKeys()){
//    					System.out.println("name = "+ name + "  value = " + info.getInfo().get(name) + " class = " + info.getInfo().get(name).getClass());
//    				}
        			acroFields.setField(businessInput.getName(), businessInput.getInput());
            		acroFields.setFieldProperty(businessInput.getName(), "fflags", PdfFormField.FF_READ_ONLY, null);
        		} else if(type == BusinessInput.Type.SIGN){
//        			acroFields.setField(businessInput.getName(), "/sdcard/demo_blue.jpg");
//            		acroFields.setFieldProperty(businessInput.getName(), "fflags", PdfFormField.FF_READ_ONLY, null);
//        			FieldPosition position = acroFields.getFieldPositions(businessInput.getName()).get(0);
//        			Rectangle rec = position.position;
//        			System.out.println(position.page + "   " + rec.getLeft() + "   " + rec.getBottom() + "   "
//        					+ rec.getWidth() + "   " + rec.getHeight());
//        			
//        			Image image = Image.getInstance("/sdcard/demo_blue.jpg");
//        			image.setAbsolutePosition(rec.getLeft(), rec.getBottom());
//        			image.scaleAbsolute(rec.getWidth(), rec.getHeight());
//        			PdfContentByte canvas =  stamper.getOverContent(position.page);
//        			canvas.addImage(image);
//        			acroFields.removeField(businessInput.getName());
        		}
        		


        	}
        }
        
        stamper.close();
        
        
        
//       
//        
//        form.setField("name", "刘浪");
//        FieldPosition fPosition = form.getFieldPositions("name").get(0);
//        float f = fPosition.position.getTop();
//        form.setField("city", "成都");
//        form.setField("check", "是");
//        form.setField("Group5", "v2");
//        
//        
//        
//        
//        canvas.beginText();
//        canvas.setFontAndSize(bf_helv, 12);
//        canvas.showTextAligned(Element.ALIGN_LEFT, "dfasdf", fPosition.position.getLeft(), fPosition.position.getBottom(), 0);
//        form.removeField("name");
//        canvas.endText();
//        stamper.close();
		
	}
	
	public static void fillPDFSignature(String signaturePath, String srcPDFPath, 
			String toPDFPath) throws Exception{		
        
        File file = new File(toPDFPath);
        if(file.exists()){
        	file.delete();
        }
        file.createNewFile();
      
        PdfReader reader = new PdfReader(srcPDFPath);       
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(toPDFPath));
        AcroFields acroFields = stamper.getAcroFields();

		FieldPosition position = acroFields.getFieldPositions(Constants.FIELD_NAME_SIGNATURE).get(0);
		Rectangle rec = position.position;
		System.out.println(position.page + "   " + rec.getLeft() + "   " + rec.getBottom() + "   "
				+ rec.getWidth() + "   " + rec.getHeight());
		
		Image image = Image.getInstance(signaturePath);
		image.setAbsolutePosition(rec.getLeft(), rec.getBottom());
		image.scaleAbsolute(rec.getWidth(), rec.getHeight());
		PdfContentByte canvas =  stamper.getOverContent(position.page);
		canvas.addImage(image);
		acroFields.removeField(Constants.FIELD_NAME_SIGNATURE);      
        stamper.close();

	}

}
